package sporteam.web.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;


public class CompetitionWidget extends Composite {

	public CompetitionWidget(Competition competition) {
		HorizontalPanel panel = new HorizontalPanel();
		
		FlexTable competitionTable = new FlexTable();
		
		competitionTable.setCellSpacing(2);
		competitionTable.setStyleName("competitionWidget");
		
		//Assemble competitionTable
		//
		competitionTable.setText(0, 0, getUserNameByID(competition.getRunner1Id()));
		competitionTable.setText(0, 1, "Competition Type");
		competitionTable.setText(0, 2, getUserNameByID(competition.getRunner2Id()));
		//
		competitionTable.setWidget(1, 0, getUserPictureByID(competition.getRunner1Id()));
		competitionTable.setText(1, 1, "Type");
		competitionTable.setWidget(1, 2, getUserPictureByID(competition.getRunner2Id()));
		//
		competitionTable.setText(2, 0, "Score: " + competition.getRunner1Score());
		competitionTable.setText(2, 2, "Score: " + competition.getRunner2Score());
		
		

		// Assemble the main panel
		panel.add(competitionTable);
		
		// All composites must call initWidget() in their constructors.
		initWidget(panel);

		// Give the overall composite a style name.
		
	}

	private String getUserNameByID(String id) {
		if(Sporteam_web.miniUser.getFacebook_id().equals(id))
			return Sporteam_web.miniUser.getFirst_name() + " " + Sporteam_web.miniUser.getLast_name();
		
		for(String friend : Sporteam_web.friends_list){
			if(friend.split(";")[0].trim().equals(id)){
				return friend.split(";")[1].trim();
			}
		}
		return null;
	}

	private Widget getUserPictureByID(String id) {	
		Image userImage = new Image();
		String userImageURL = "https://graph.facebook.com/"
				+ id + "/picture?type=square";
		userImage.setUrl(userImageURL);
		
		return userImage;
	}


	
	
}
